package com.ruoyi.project.module.lyq.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YhInstructor;
import com.ruoyi.project.service.IYhInstructorService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

/**
 * 指导教师存放表Controller
 * 
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/lyq/instructor")
public class LyqInstructorController extends BaseController
{
    @Autowired
    private IYhInstructorService yhInstructorService;

    /**
     * 查询指导教师存放表列表
     */
//    @RequiresPermissions("lyq:instructor:list")
    @GetMapping("/list")
    public TableDataInfo list(YhInstructor yhInstructor)
    {
        startPage();
        yhInstructor.setStudentId(getUsername());
        List<YhInstructor> list = yhInstructorService.selectYhInstructorList(yhInstructor);
        return getDataTable(list);
    }

    /**
     * 导出指导教师存放表列表
     */
//    @RequiresPermissions("lyq:instructor:export")
    @Log(title = "指导教师存放表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhInstructor yhInstructor)
    {
        List<YhInstructor> list = yhInstructorService.selectYhInstructorList(yhInstructor);
        ExcelUtil<YhInstructor> util = new ExcelUtil<YhInstructor>(YhInstructor.class);
        util.exportExcel(response, list, "指导教师存放表数据");
    }

    /**
     * 获取指导教师存放表详细信息
     */
//    @RequiresPermissions("lyq:instructor:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yhInstructorService.selectYhInstructorById(id));
    }

    /**
     * 新增指导教师存放表
     */
//    @RequiresPermissions("lyq:instructor:add")
    @Log(title = "指导教师存放表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhInstructor yhInstructor)
    {
//        LoginUser loginUser = getLoginUser();
//        SysUser sysUser = loginUser.getSysUser();
////        System.out.println(sysUser.getNickName());
//
//
//        YhInstructor instructor = yhInstructorService.selectYhInstructorByStuId(getUsername());
//
//        if(Objects.isNull(instructor))
//        {yhInstructor.setStudentId(sysUser.getUserName());
//            yhInstructor.setName(sysUser.getNickName());
//            return toAjax(yhInstructorService.insertYhInstructor(yhInstructor));}
//        else {
//
//            throw new ServiceException("已添加指导老师信息");
//        }
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
//System.out.println(sysUser.getNickName());
        yhInstructor.setStudentId(sysUser.getUserName());
        yhInstructor.setName(sysUser.getNickName());
        String userId = getUsername();
        yhInstructor.setStudentId(userId);
        YhInstructor yi = yhInstructorService.selectYhInstructorByStuId(userId);
//System.out.println(yi);

        if(yi==null){
            return toAjax(yhInstructorService.insertYhInstructor(yhInstructor));
        }else{
            yhInstructor.setId(yi.getId());
            //return toAjax(yhInstructorService.updateYhInstructor(yhInstructor));
            return toAjax(yhInstructorService.updateYhInstructor(yhInstructor));
        }



    }

    /**
     * 修改指导教师存放表
     */
//    @RequiresPermissions("lyq:instructor:edit")
    @Log(title = "指导教师存放表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhInstructor yhInstructor)
    {
        return toAjax(yhInstructorService.updateYhInstructor(yhInstructor));
    }

    /**
     * 删除指导教师存放表
     */
//    @RequiresPermissions("lyq:instructor:remove")
    @Log(title = "指导教师存放表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhInstructorService.deleteYhInstructorByIds(ids));
    }


    @GetMapping("/list/t")
    public YhInstructor searchT(){
        String userId = getUsername();

        YhInstructor yi = yhInstructorService.selectYhInstructorByStuId(userId);
        if(yi==null)
        {return new YhInstructor();}
        else {
            return yi;
        }
        

    }
}
